<?php
/**
* File that contains Application_Form_Admin_insertColor class.
* @package busmap
*/

/**
* Application_Form_Admin_insertColor class generates a form used to insert a new color.
* Form has a text field for the name of the color.
* There are three text fields for the red, green and blue value of the color.
* @package busmap
*/
class Application_Form_Admin_insertColor extends Zend_Form
{
	/**
	* Initialize function generates and adds the elements described in the class description.
	*/
    public function init()
    {
        $this->setMethod('post');

		$this->addElement(
            'text', 'name', array(
                'required' => true,
                'filters'    => array('StringTrim'),
				'class' => 'bold-text'
            ));
		$this->name->addErrorMessage('Värvi nimi ei saa olla tühi');
		$this->name->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell'));

        $this->addElement(
            'text', 'redValue', array(
                'required' => true,
				'class' => 'small-data',
				'validators' => array('Digits')
            ));
		$this->redValue->addErrorMessage('Punane toon peab olema number');
		$this->redValue->addDecorator('HtmlTag', array('tag'=>'div', 'openOnly'=>true, 'class'=>'data-listing data-listing-cell'));

		$this->addElement(
            'text', 'greenValue', array(
                'required' => true,
				'class' => 'small-data',
				'validators' => array('Digits')
            ));
		$this->greenValue->addErrorMessage('Roheline peab olema number');

		 $this->addElement(
            'text', 'blueValue', array(
                'required' => true,
				'class' => 'small-data',
				'validators' => array('Digits')
            ));
		$this->blueValue->addErrorMessage('Sinine toon peab olema number');
		$this->blueValue->addDecorator('HtmlTag', array('tag'=>'div', 'closeOnly'=>true, 'class'=>'data-listing data-listing-cell'));

		 $this->addElement(
            'submit', 'addNew', array(
                'required' => false,
				'label' => 'Lisa',
				'class' => 'submit-data'
            ));
		$this->addNew->addDecorator('HtmlTag', array('tag'=>'div', 'class'=>'data-listing data-listing-cell-button'));

		$this->clearElementLabels();
		
    }

	/**
	* This function overwrites the Zend_Form isValid function.
	* Form data is sent to the parent's isValid function too, but the red, green and blue value are checked to be between 0 and 255.
	*/
	public function isValid($form){
		if(!$form) return;

		$valid = parent::isValid($form);

		if(array_key_exists('redValue', $form) && $form['redValue'] > 255 || $form['redValue'] < 0){
			$this->redValue->addError('Punase tooni väärtus peab jääma vahemikku [0, 255]');
			$valid = false;
		}

		if(array_key_exists('greenValue', $form) && $form['greenValue'] > 255 || $form['greenValue'] < 0){
			$this->greenValue->addError('Rohelise tooni väärtus peab jääma vahemikku [0, 255]');
			$valid = false;
		}

		if(array_key_exists('blueValue', $form) && $form['blueValue'] > 255 || $form['blueValue'] < 0){
			$this->blueValue->addError('Sinise tooni väärtus peab jääma vahemikku [0, 255]');
			$valid = false;
		}
		return $valid;

	}

	/**
	* This function clears the label, DtDd and error decorators from all of the elements.
	* Errors are shown elsewhere in the view.
	*/
	public function clearElementLabels(){
		$elements = $this->getElements();
		foreach($elements as $element){
			$element->removeDecorator('label')->removeDecorator('DtDdWrapper')->removeDecorator('Errors');
		}
	}

}